//
//  ViewController.m
//  NSDictionary
//
//  Created by apple on 15/8/20.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

#if 0
//1------------------------------------------------------------1
    //以字面常量形式创建不可变字典，键值对之间用冒号分割；
    //不同的键值对之间用逗号隔开；
    
    NSDictionary *dict = @{@"name":@"frank",@"age":@23};
//1-------------------------------------------------------1

    //2------------------------------------------------------------2
    // 注意创建到形式：值，键，值，键，值，键。。。
    
    NSDictionary *dict2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"frank",@"name",@23,@"age", nil];
//2------------------------------------------------------------2
    
    //3------------------------------------------------------------3
    //注意键值对数组的相应匹配
    NSDictionary *dict3 = [[NSDictionary alloc] initWithObjects:@[@"frank",@23] forKeys:@[@"name",@"age"]];
//3------------------------------------------------------------------------------------------3
    
    //4---------------------------------------------------------4
    //用类方法创建不可变字典;
    NSDictionary *dict4 = [NSDictionary dictionaryWithObject:@"frank" forKey:@"name"];
    NSDictionary *dict5 = [NSDictionary dictionaryWithObjectsAndKeys:@"frank",@"name",@23,@"age", nil];
//创建一个空字典［注意］空字典的意思不是说字典的值为nil，而是说字典这个对象是存在的，只不过里面没有键值对；
    //------------------------------------------------------------4
#endif
    
#if 0
    
    //------------------------------键值对的个数；
    NSDictionary *dict = @{@"name":@"frank",@"age":@23};
    NSInteger eleCount = [dict count];
    NSLog(@"%ld",eleCount);
    
    //返回字典中所有的key，但不会保证按原本的key顺序排列；
    
    NSArray *keyArray = [dict allKeys];
    NSLog(@"%@",keyArray);
    
    NSLog(@"------------------------------");
    
    //返回字典中所有的value，但不会保证它的顺序是否发生变化；
    NSArray *valuesArray = [dict allValues];
    NSLog(@"%@",valuesArray);
    
#endif
    
#if 0
    
    //返回所有某个值对应的key；
    //字典中，key的值要求唯一，但值却没做什么要求，这也就意味着某个值可以重复的出现；
    NSDictionary *dict = @{@"name":@"kuang",@"age":@"kuang",@"man":@"women"};
    NSArray *str = [dict allKeysForObject:(@"kuang")];
    NSLog(@"%@",str);
    
    //已知key获取value；
    NSString *str1 = [dict objectForKey:@"man"];
    //简便方法；
    NSString *str2 = dict[@"man"];
    NSLog(@"%@",str2);
#endif

#if 0
    //------------------------------------------------------------------------------------------
    //关于枚举／遍历；
    
    //一次输出key的值，但不会保证按顺序输出；
    NSDictionary *dict = @{@"name":@"kuang",@"age":@23};
    NSEnumerator *keyEnu = [dict keyEnumerator];
    id obj;
    while (obj = [keyEnu nextObject]) {
        NSLog(@"key--> %@",obj);
    }
    
    //遍历键值对；［即都用一次］；，
    [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        NSLog(@"key:%@ value:%@",key,obj);
    }];
//    ------------------------------------------------------------------------------------------
#endif
    
#if 0
    //根据对value的升序排序后，从而返回其对应的key，并用数组保存；
    //这里值得注意的是：不是所有的对象都能实现compare的方法，也就是说如果某值为string类型，运用此方法则可能crash
    
    NSDictionary *dict = @{@"name":@"frank",@"age":@23,@"man":@"women"};
    NSArray *array = [dict keysSortedByValueUsingSelector:@selector(compare:)];
    [dict keysSortedByValueUsingComparator:^NSComparisonResult(id obj1,id obj2)
    {
    
        if ([obj1 length] > [obj2 length])
        {
            return NSOrderedDescending;
        }
        else if ([obj1 length] < [obj2 length])
        {
            return NSOrderedDescending;
        }
        else
        {
            return NSOrderedSame;
        }
    }];
    
    
    
//    NSDictionary*P = @{@"a":@" you",@"b":@"me",@"c":@"sadsada",@"d":@"hehe"};
//    
//    [P setValue:@"asdasdad" forKey:@"e"];
//    
//    NSArray*name = [P allKeys];
//    NSLog(@"%@",name);
    
#endif
    
    
#if 0
    
#endif
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
